package com.wpre.rabbitmqspringboot.controller;

import com.wpre.rabbitmqspringboot.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2025-03-31
 * Time: 19:53
 */
@RestController
@RequestMapping("/producer")
public class ProducerController {

    //使用RabbitTemplate,进行消息的发送
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, ("Hello Spring AMQP" + i));
        }
        return "发送成功";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "Hello Spring qmqp:fanout");
        return "发送成功";
    }


    @RequestMapping("/direct/{routingKey}")
    public String direct(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey, "Hello Spring qmqp:direct" + "routingKey is " + routingKey);
        return "发送成功";
    }

    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "Hello Spring qmqp:topic" + "routingKey is " + routingKey);
        return "发送成功";
    }
}
